package net.fdukedom.epicurus.presentation.actions.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.fdukedom.epicurus.presentation.actions.AbstractAction;
import net.fdukedom.epicurus.presentation.SessionData;
import net.fdukedom.epicurus.domain.entity.User;
import net.fdukedom.epicurus.domain.util.UserHelper;

public class ProfileAction extends AbstractAction<ProfileForm> {

    @Override
    protected ActionForward doExecute(ActionMapping mapping, ProfileForm form, HttpServletRequest request, HttpServletResponse response) {
        User user = SessionData.getInstance(request).getUser();
        user.setName(form.getUserName());
        user.setSex(form.getSex());
        if (form.getNonEmptyPassword().length() > 0) {
            user.setPassword(form.getPassword());
        }
        UserHelper.getInstance().saveUser(user);
        return mapping.findForward("success");
    }

}
